package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.*;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author Guoyh
 * @since 2022-08-21
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void saveSetmal(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page selectByPage(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page(page, pageSize);
        Page<SetmealDto> page1 = new Page();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name).orderByDesc(Setmeal::getUpdateTime);
        setmealMapper.selectPage(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo, page1, "records"); // 泛型不一样，一个是Setmeal 一个是SetmealDto

        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> list1 = new ArrayList<>();

        records.forEach(
                new Consumer<Setmeal>() {
                    @Override
                    public void accept(Setmeal setmeal) {
                        SetmealDto setmealDto = new SetmealDto();
                        Long id = setmeal.getCategoryId();
                        Category category = categoryMapper.selectById(id);

                        if (category != null) {
                            String categoryName = category.getName();
                            setmealDto.setCategoryName(categoryName);
                            BeanUtils.copyProperties(setmeal, setmealDto);
                            list1.add(setmealDto);
                        }
                    }
                }
        );

        page1.setRecords(list1);
        return page1;
    }

    @Transactional
    @Override
    public void updateStatus(int flag, List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            Setmeal setmeal = setmealMapper.selectById(ids.get(i));
            setmeal.setStatus(flag);
            this.updateById(setmeal);
        }

//        for (int i = 0; i < ids.size(); i++) {
//            UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.set("status",flag);
//            updateWrapper.eq("id",ids.get(i));
//            setmealMapper.update(null,updateWrapper);
//        }
    }

    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);
        Integer count = setmealMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        setmealMapper.deleteBatchIds(ids);

        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(queryWrapper1);
    }

    @Override
    public SetmealDto selectById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        setmealDto.setCategoryName(category.getName());
        return setmealDto;
    }

    @Transactional
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        setmealMapper.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishService.saveBatch(setmealDishes);
    }
}
